@extends('layout')
@section('content')
    <nav class="breadcrumb">
        <i class="Hui-iconfont">&#xe67f;</i>
        当前位置: <span class="c-gray en">&gt;</span>
        渠道管理 <span class="c-gray en">&gt;</span>
        渠道列表
    </nav>
    <div class="page-container">
        <div class="text-c">
            <form action="" method="get" id="search">
                渠道名称：
                <input type="text" name="name" id="name"
                       value="{{ $query['name'] }}"
                       placeholder="" style="width:100px" class="input-text">
                <span class="select-box inline">
                所属父渠道：
                    <select name="pid" class="select">
                        <option value="0">--请选择--</option>
                        @foreach($topChannel as $channel)
                            <option value="{{ $channel->id }}"
                                    @if($query['pid'] == $channel->id) selected @endif>
                                {{ $channel->name }}</option>
                        @endforeach
                    </select>
                </span>
                一级渠道：
                <input type="text" name="one_channel" id="one_channel"
                       value="{{ $query['one_channel'] }}"
                       placeholder="" style="width:100px" class="input-text">
                二级渠道：
                <input type="text" name="two_channel"
                       value="{{ $query['two_channel'] }}"
                       id="two_channel" placeholder="" style="width:100px" class="input-text">
                <span class="select-box inline">
                合作方式：
                <select name="cooperate" class="select" style="width: 98px;">
                    <option value="0">--请选择--</option>
                    @foreach($cooperates as $channel)
                        <option value="{{ $channel->id }}"
                                @if($query['cooperate'] == $channel->id) selected @endif>
                            {{ $channel->name }}</option>
                    @endforeach
                </select>
                </span>
                <button name="" id="" class="btn btn-success" type="submit">查询</button>
                {{--<button name="" id="" class="btn btn-info" type="reset"></button>--}}
                <a class="btn btn-info" style="line-height:1.6em;margin-top:3px" id="btn-refresh" href="javascript:;" onclick="location.replace('{{ url('nonPublicVersion/channel') }}')" title="重置" >重置</a>
            </form>
        </div>


        <div class="cl pd-5 bg-1 bk-gray mt-20">
            <span class="r">
                <a class="btn btn-primary radius" data-title="新增" onclick="openDialog('','{{ url('nonPublicVersion/channel/create') }}','','',true)" href="javascript:;">
                     新增
                </a>

                <a class="btn btn-primary radius"
                   data-title="删除" onclick="del()" href="javascript:;">
                     删除
                </a>
            </span>
        </div>
        <div class="mt-20">
            <table class="table table-border table-bordered table-bg table-hover table-sort">
                <thead>
                <tr class="text-c">
                    <th width="25"><input type="checkbox" name="" value=""></th>
                    <th width="80">序号</th>
                    <th>渠道名称</th>
                    <th width="80">所属父渠道</th>
                    <th width="80">一级渠道</th>
                    <th width="120">二级渠道</th>
                    <th width="75">合作方式</th>
                    <th width="75">ID</th>
                    <th width="120">操作</th>
                </tr>
                </thead>
                <tbody>
                    @foreach($lists as $list)
                        <tr class="text-c">
                            <td><input type="checkbox" value="{{ $list->id }}" name="ids[]"></td>
                            <td>{{ $list->id }}</td>
                            <td class="text-l">{{ $list->name }}</td>
                            <td>{{ $list->father }}</td>
                            <td>{{ $list->one_channel_name }}</td>
                            <td>{{ $list->two_channel_name }}</td>
                            <td>{{ $list->cooperate }}</td>
                            <td>{{ $list->unique_id }}</td>
                            <td class="f-14 td-manage">
                                <a style="text-decoration:none" class="ml-5" onClick="openDialog('','{{ url('nonPublicVersion/channel/' . $list->id. '/edit') }}','','',true)" href="javascript:;" title="编辑">
                                    编辑</a>
                                <a style="text-decoration:none" class="ml-5"
                                   onClick="deleteChannel('{{ $list->id }}')" href="javascript:;" title="删除">
                                    删除
                                </a>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>

            <div id="page"  class="r" style="margin-top: 20px;"></div>
            <div  class="r" style="margin-top: 20px;">
                {{--<span class="l" style="margin-top: 4px;">共有数据：<strong>{{ $lists->total() }}</strong> 条</span>--}}
                <span class="l">
                    每页显示:<input type="text" id="paginate" value="{{$pageSize}}" class="input-text" style="width: 30px;margin-left: 20px;height: 28px;">
                    <input type="button" class="btn go" value="GO" style="width: 50px;height: 28px;">
                </span>
            </div>


        </div>
    </div>
@endsection
@section('script')
<script type="application/javascript">
    laypage({
        cont: $('#page'), //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：&lt;div id="page1">&lt;/div>
        pages: '{{ $lists->totalPage }}', //通过后台拿到的总页数
        curr: '{{ $lists->currentPage() }}', //初始化当前页
        last:'{{ $lists->lastPage() }}',
        jump:function (e,first) {
            var total = '{{ $lists->total() }}';
            var totalPage= '{{ $lists->totalPage }}';
            var paginate = $('#paginate').val();
            if(e.curr > Math.ceil( total / paginate)){
                e.curr = Math.ceil( total / paginate);
            }
            if (!first){
                window.location.href = '{{ $lists->url . '?page=' }}' + e.curr + '&paginate=' + paginate +'&'+'{{ http_build_query($query) }}';
            }
        }
    });

    function deleteChannel (id) {
        var index = layer.confirm('您是否确定要删除选中的条目？', {
            btn: ['确认','取消'] //按钮
        }, function(){
            request('channel/'+id,function (data) {
                layer.close(index);
                $("#btn-refresh").click();
            },{},'DELETE');
        });
    }

    function del() {
        var objs = $("input[name='ids[]']");
        var arr = [];
        $.each(objs,function (k,v) {
            if ($(this).is(':checked')){
                var val = $(this).val();
                arr.push(val);
            }
        });
        if(arr == ''){
            layer.alert('请选择要删除的条目');
            return;
        }
        var index = layer.confirm('您是否确定要删除选中的条目？', {
            btn: ['确认','取消'] //按钮
        }, function(){
            request('channel/'+arr[0],function (data) {
                layer.close(index);
                $("#btn-refresh").click();
            },{ids:arr.toString()},'DELETE',function () {

                layer.close(index);
                $("#btn-refresh").click();
            });
        });
    }
    $(".go").click(function () {
        var paginate = $('#paginate').val();
        window.location.href = '{{ $lists['url'] }}' + '?paginate=' + paginate;
    });

    $("#search").submit(function () {
        var one_channel = $("#one_channel").val();
        var two_channel = $("#two_channel").val();
        var name = $("#name").val();

        if (one_channel.length > 50){
            layer.confirm('一级渠道不能超过50字符', {
                btn: ['确定','取消'] //按钮
            });
            return false;
        }

        if (name.length > 50){
            layer.confirm('渠道名称不能超过50字符', {
                btn: ['确定','取消'] //按钮
            });
            return false;
        }

        if (two_channel.length > 50){
            layer.confirm('二级渠道不能超过50字符', {
                btn: ['确定','取消'] //按钮
            });
            return false;
        }

        return true;
    });
</script>
@endsection